import { BaseService } from "./baseService";
import { Condition } from "../domain/models/condition";
import { ApiDataContext } from "../domain/apiDataContext";
import { Transaction } from "tiny-entity2";

export class ConditionService extends BaseService {
    CreateCondition(entity: Condition, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new Condition();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    CreateConditions(datas: Condition[], ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            return ctx.CreateBatch(datas.map(x =>new Condition().ConverToEntity<Condition>(x)));
        });
    }
    UpdateCondition(entity: Condition, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new Condition();
            d.ConverToEntity(entity);
            return ctx.Update(d);
        });
    }
    GetConditionById(id: string) {
        let ctx = new ApiDataContext();
        return ctx.Condition.First(x => x.id == $args1, { $args1: id });
    }
    QueryCondition(params) {
        let ctx = new ApiDataContext();
    }

    DeleteCondition(id: string, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            return ctx.Delete<Condition>(x => x.id == $args1, ctx.Condition, { $args1: id });
        });
    }

    DeleteConditionByObjectId(objectId: string, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            return ctx.Delete<Condition>(x => x.objectId == $args1, ctx.Condition, { $args1: objectId });
        });
    }
}